<template>
  <h3>类型设置</h3>
  <!-- 搜索栏 -->
  <el-row style="margin-top: 10px">
    <el-col :span="24">
      <el-form :model="typeQuery" inline>
        <el-form-item label="类型编码">
          <el-input v-model="typeQuery.typeEncode" placeholder="请输入类型编码"/>
        </el-form-item>
        <el-form-item label="类型名称">
          <el-input v-model="typeQuery.typeName" placeholder="请输入类型名称"/>
        </el-form-item>
        <el-form-item label="项目类型">
          <el-select v-model="typeQuery.typeType" placeholder="请选择保养维护类型" style="width: 200px">
            <el-option label="全部" :value="0"/>
            <el-option label="定期维护" :value="1"/>
            <el-option label="按使用次数维护" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadTypes">搜索</el-button>
          <el-button @click="reloadTypes">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
  <!-- 工具栏 -->
  <el-row>
    <el-col :span="24">
      <el-button type="primary" @click="toAdd">新增</el-button>
      <el-button type="primary" @click="toEdits">修改</el-button>
      <el-button type="primary" @click="toDelete">删除</el-button>
    </el-col>
  </el-row>
  <!-- 数据表 -->
  <el-row style="margin-top: 10px">
    <el-col :span="24">
      <el-table
          :data="types"
          border
          stripe
      ><!-- stripe可以为表格添加斑马纹样式，默认false -->
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="typeEncode" label="类型编码" width="150" align="center">
          <template #default="scope">
            <el-link @click="showDetail(scope.row.typeId)" type="primary">{{ scope.row.typeEncode }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="typeName" label="类型名称" width="150" align="center"/>
        <el-table-column prop="typeStatus" label="是否编码管理" width="150" align="center">
          <template #default="scope">
            <el-tag type="primary" v-if="scope.row.typeStatus === 1">是</el-tag>
            <el-tag type="danger" v-else>否</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="typeType" label="保养维护类型" width="150" align="center">
          <template #default="scope">
            <el-tag v-if="scope.row.typeType == 1">定期维护</el-tag>
            <el-tag v-else-if="scope.row.typeType == 2">按使用次数维护</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="typeFrequency" label="保养周期" width="150" align="center"/>
        <el-table-column prop="typeRemark" label="备注" width="250" align="center"/>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button-group>
              <el-button size="small" type="primary" @click="editType(scope.row.typeId)">修改</el-button>
              <el-button size="small" type="danger" @click="deleteType(scope.row)">删除</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
  </el-row>
  <!-- 新增工装夹具类型弹窗 -->
  <types-add :flag="addType" @cancelAdd="cancelAdd" @addComplete="loadTypes"/>
  <!-- 编辑工装夹具类型弹窗 -->
  <types-edit :typeId="editTypeId" @cancelEdit="cancelEdit" @editComplete="loadTypes"/>
  <!-- 详情工装夹具类型弹窗 -->
  <types-detail :typeId="detailTypeId" @cancelDetail="cancelDetail"/>
</template>

<script setup>

// 封装查询条件
import {onMounted, ref} from "vue";

import {ElMessage, ElMessageBox} from "element-plus";
import typeRequest from "@/api/type.js";
import TypesAdd from "@/views/tools/types-add.vue";
import TypesDetail from "@/views/tools/types-detail.vue";
import TypesEdit from "@/views/tools/types-edit.vue";

// 封装查询条件
let typeQuery = ref({});
// 封装查询的结果集合
let types = ref([]);
// 新增的addType
let addType = ref(-1);
// 编辑的editTypeId
let editTypeId = ref(-1);
// 详情的detailTypeId
let detailTypeId = ref(-1);

function cancelDetail(){
  detailTypeId.value = -1;
}

function showDetail(typeId) {
  detailTypeId.value = typeId;
}
function toAdd(){
  addType.value = 1;
}
function cancelAdd(){
  addType.value = -1;
}
function loadTypes(){
  typeRequest.loadTypes(typeQuery.value,res=>{
    types.value = res.data;
  })
}
function reloadTypes(){
  typeQuery.value.typeEncode = null;
  typeQuery.value.typeName = null;
  typeQuery.value.typeType = 0;
  typeRequest.loadTypes(typeQuery.value,res=>{
    types.value = res.data;
  })
}

function editType(typeId){
  editTypeId.value = typeId;
}
function cancelEdit() {
  editTypeId.value = -1;
}
function deleteType(type){
  ElMessageBox.confirm(
      '您确定要将设备[' + type.typeEncode + ']删除吗？',
      'Warning',
      {
        confirmButtonText: '是的',
        cancelButtonText: '点错了',
        type: 'warning',
      }
  ).then(() => {
    typeRequest.delete(type.typeId, res => {
      ElMessage({
        type: "success",
        message: "操作完成"
      });
      loadTypes();
    })
  }).catch(() => {
  });
}

onMounted(()=>{
  loadTypes();
})
</script>

<style scoped>

</style>